package com.songtech.auth.user.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.songtech.auth.role.dao.AuthRoleMapper;
import com.songtech.auth.role.model.Role;
import com.songtech.auth.user.dao.AuthUserMapper;
import com.songtech.auth.user.model.User;
import com.songtech.auth.user.service.UserService;
import com.songtech.auth.util.CommState;
import com.songtech.auth.util.IdGenerator;
import com.songtech.auth.util.MD5Util;
import com.songtech.base.exception.BusinessException;
import com.songtech.base.universal.dao.BaseDao;

@Service
public class UserServiceImpl implements UserService {
	
	private static Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
	
	@Autowired
	private AuthUserMapper userMapper;
	
	@Autowired
	private AuthRoleMapper roleMapper;

	@Resource(name = "com.songtech.auth.user.model.User")
	private BaseDao<User> userDao;
	
	@Override
	public int addUser(User user) {
		//用户登录名为空
		if(StringUtils.isEmpty(user.getLoginName())){
			return 0;
		}
		//密码为空
		if(StringUtils.isEmpty(user.getPassword())){
			return -1;
		}
		//查询是否已经存在当前登录名的用户
		//User qUser = new User();
		//qUser.setLoginName(user.getLoginName());
		User resultUser = userMapper.queryUser(user);
		if(resultUser!=null){
			return -2;
		}
		user.setId(IdGenerator.UUID());
		try {
			user.setPassword(MD5Util.encode(user.getPassword()));
		} catch (Exception e) {
			log.error("密码转换失败:"+e.getMessage());
			e.printStackTrace();
		}
		user.setIsValid(CommState.EFFECTIVE);
//		userMapper.insert(user);
		try {
			userDao.insert(user);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new BusinessException("新增用户失败");
		}
		return 1;
	}

	@Override
	public int updateUser(User user) {
		int result = 0;
		try {
			result = userDao.update(user);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public int updateUserById(User user) {
		//ID为空
		if(StringUtils.isEmpty(user.getId())){
			return 0;
		}
		User result = userMapper.queryUserById(user.getId());
		//没有查询到对应用户信息
		if(result==null){
			return -1;
		}else{
			//用户登录名不能修改
			if(!result.getLoginName().equals(user.getLoginName())){
				return -2;
			}
			if(StringUtils.isEmpty(user.getIsValid())){
				user.setIsValid(CommState.INVALID);
			}
		}
		userMapper.updateById(user);
		return 1;
	}

	@Override
	public User queryUser(User user) {
		return userMapper.queryUser(user);
	}
	@Override
	public List<User> queryUserList(User user) {
		return userMapper.queryUserList(user);
	}
	
	@Override
	public User queryUserById(String id) {
		return userMapper.queryUserById(id);
	}
	@Override
	public PageInfo<User> getAll(User user,int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<User> list = userMapper.getAll(user);
		return new PageInfo<User>(list);
	}

	@Override
	public int delete(String id) {
		userMapper.delete(id);
		return 1;
	}

	@Override
	public List<User> getAllUser() {
		return userMapper.getAllUser();
	}

	@Override
	public int enabledUser(String id) {
		User user = userMapper.queryUserById(id);
		user.setIsValid(CommState.EFFECTIVE);
		userMapper.update(user);
		return 1;
	}

	@Override
	public int disabledUser(String id) {
		User user = userMapper.queryUserById(id);
		user.setIsValid(CommState.INVALID);
		userMapper.update(user);
		return 1;
	}

	@Override
	public int updatePassword(String loginName, String oldPassword, String newPassword) {
		User qUser = new User();
		qUser.setLoginName(loginName);
		User result = userMapper.queryUser(qUser);
		if(result==null){
			return -1;
		}else{
			String password = "";
			try {
				password = MD5Util.encode(oldPassword);
			} catch (Exception e) {
				log.error("转换失败:"+e.getMessage());
				e.printStackTrace();
			}
			if(!(result.getPassword().equals(password))){
				return 0;
			}
			try {
				qUser.setPassword(MD5Util.encode(newPassword));
			} catch (Exception e) {
				log.error("新密码转换失败:"+e.getMessage());
				e.printStackTrace();
			}
		}
		userMapper.updatePassword(qUser);
		return 1;
	}

	@Override
	public User queryUserByName(String userName) {
		return userMapper.queryUserByName(userName);
	}

	@Override
	public List<Role> getUserRoles(String loginName) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("loginName", loginName);
		return roleMapper.getUserRoles(map);
	}

}
